<?php
/**
 * Enter description here...
 *
 * @package if
 * @subpackage module.helper.controllers
 * @since 1.0
 */
class IfHelperLogController extends IHelperController {
	function doIndex() {
		$logFile = o("@log.file");
		$filter = new ISimpleLogFilter();
		$filter->doFilter($logFile);
		$logFile = IF_APP_ROOT . "/logs/" . $logFile;
		$logs = array();
		if (is_file($logFile)) {
			$content = file_get_contents($logFile);
			preg_match_all("/(^|\n)[\d+\.]+ - ([^-]+) - .+ - .+ - .+(?=\n[\d+\.]+|$)/sU", $content, $matches);
			$logs = $matches[0];
			$lastTime = 0;
			$color = $this->_randomColor();
			foreach ($logs as $key => $log) {
				$time = strtotime($matches[2][$key]);
				if ($lastTime > 0 && $time - $lastTime <= 2) {
					$logs[$key - 1] = "<div style=\"background-color:{$color}\">" . $logs[$key - 1] . "</div>\n";
					$logs[$key] = "<div style=\"background-color:{$color}\">" . $logs[$key] . "</div>\n";
				}
				else {
					$logs[$key] = "<div>" . $log . "</div>\n";
					$color = $this->_randomColor();
				}
				$lastTime = $time;
			}
			$logs = array_reverse($logs);
		}
		$this->logs = $logs;
	}
	
	private function _randomColor() {
		$colors = array("#E9967A", "#FAEBD7", "#BDB76B", "#7FFFD4", "#8FBC8B", "#B8860B", "#FFE4C4");
		return $colors[rand(0, 5)];
	}
}

?>